<?php
namespace utils;

use JsonPath\JsonObject;
use store\FileBlockWriter;
use store\FileBlockReader;
use store\DfsBlockWriter;
use store\DfsBlockReader;
use store\MinioBlockReader;
use store\MinioBlockWriter;
use utils\FileTool;

class ConfigReader
{
    var $m_files;
    var $m_appPath;//项目路径 /www/up6/
    
    
    //构造函数
    function __construct()
    {   
        //定位到/config/
        $root = str_replace("\\", "/", __FILE__);
        $pos = strpos($root,"/db/utils/");        
        //
        $root = substr($root, 0,$pos+1);
        //=>web根目录
        $this->m_appPath = $root;
        
        $ps = $this->m_appPath . "config/config.json";
        
        $data = FileTool::readAll($ps);
        $this->m_files = new JsonObject($data);   
    }
    
    //析构函数
    function __destruct()
    {
    }
    
    /**
     * 从config.json中取文件路径，并转换成JsonPath对象
     */
    function module($name)
    {
        $p = $this->m_files->get("$.$name");
        $p = $this->m_appPath . $p[0];
        $data = FileTool::readAll($p);
        return new JsonObject($data);
    }
    
    /**
     * 读取config.json中路径值的内容
     */
    function data($name)
    {
        $p = $this->m_files->get("$.$name");
        $p = $this->m_appPath . $p[0];
        $data = FileTool::readAll($p);
        return $data;
    }

    /**
     * 取config.json中的字符串值
     */
    function readString($name)
    {
        $p = $this->m_files->get("$.$name");
        return $p[0];
    }

    public static function fastDFSConfig(){
        $cr = new ConfigReader();
        return $cr->readString("FastDFS");
    }

    /**
     * 存储类型,io,FastDFS,
     */
    public static function storageType(){
        $cr = new ConfigReader();
        return $cr->readString("Storage.type");
    }

    /**
     * 传输加密
     */
    public static function postEncrypt()
    {
        $cr = new ConfigReader();
        $encry = $cr->readString("security.encrypt");
        return StringTool::iequals($encry,"true");
    }

    /**
     * 存储加密
     */
    public static function storageEncrypt()
    {
        $cr = new ConfigReader();
        $sto = $cr->readString("Storage.encrypt");
        return $sto;
    }

    public static function blockWriter()
    {
        if(StringTool::iequals(ConfigReader::storageType(), "FastDFS")) return new DfsBlockWriter();
        if(StringTool::iequals(ConfigReader::storageType(), "minio")) return new MinioBlockWriter();
        return new FileBlockWriter();
    }

    public static function blockReader()
    {
        $st = ConfigReader::storageType();
        if( 0 == strcasecmp($st,"FastDFS") ) return new DfsBlockReader();
        if( 0 == strcasecmp($st,"minio") ) return new MinioBlockReader();
        return new FileBlockReader();
    }
}
?>